package com.interview.controller;

import com.interview.pojo.Comment;
import com.interview.service.CommentService;
import com.interview.utils.DateUtils;
import com.interview.utils.Id_Builder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService comment_service;

    //查询全部数量
    //http://localhost:10087/comment/add

    /**
     * {
     *     "context":"评论xxxxx",
     *     "reply_id":"null",
     *     "their":"text"
     * }
     * @param comment
     */
    @PostMapping("/add")
    public Comment  add(@RequestBody Comment comment){



        try {
            comment.setId(Id_Builder.nextId());
            comment.setTime(DateUtils.dateStrng());
            comment_service.add(comment);
        } catch (Exception e) {
            e.printStackTrace();
            comment=null;
            return  comment;
        }
        return  comment;

    }

    // http://localhost:10087/comment/comment_select?their=394-2021nN2kA10iP20jF29nS25w194W-293
    //http://localhost:10087/comment/comment_select?their=test
    @GetMapping("/comment_select")
    public List<Comment>    comment_select(String their){
        List<Comment> comments = comment_service.comment_select(their);
        return  comments;
    }
    //http://localhost:10087/comment/comment_select_son?their=test&&reply_id=848-2021vB2vY5oG18oN21wD10i371F-754
    @GetMapping("/comment_select_son")
    public List<Comment>    comment_select_son(String their, String reply_id){
        List<Comment> comments = comment_service.comment_select_son(their,reply_id);
        return  comments;
    }



    //http://localhost:10087/comment/count
    @GetMapping("/count")
    public  int  count(){
        return  comment_service.count();
    }

}
